import plugin from "../../lib/plugins/plugin.js";
import PluginsLoader from "../../lib/plugins/loader.js";
import common from "../../lib/common/common.js";

/**
 * 自定义指令配置
 *
 */
let config = {
  rulesDelay: 2022, // 多指令间隔延迟时间 单位毫秒，2000 = 2s 如果放到更小的时间要注意云崽的设置，云崽默认群聊中个人操作冷却时间2s
  rules: {
    "菜单别名": "#菜单",
    "触发多个功能": "#菜单 #刻晴攻略",
    "^#*支持正则$": "#菜单",
  }, // 触发多个指令时需要在指令之间添加空格，参考触发多个功能
};

export class convertRule extends plugin {
  constructor() {
    super({
      name: "自定义指令",
      dsc: "自定义指令",
      event: "message",
      priority: 1,
    });
  }
  async accept() {
    const keyWord = this.e
      .toString()
      .trim()
      /** 过滤@ */
      .replace(new RegExp("{at:" + Bot.uin + "}", "g"), "")
      .trim();

    let convert = keyWord;
    let convertKey = "";

    for (let key in config.rules) {
      if (new RegExp(key).test(keyWord)) {
        convert = config.rules[key];
        convertKey = key;
      }
    }

    if (convert !== keyWord) {
      const convertArr = convert.split(" ");

      for (let key of convertArr) {
        const tempMessage = [];

        if (this.e.message) {
          for (let val of this.e.message) {
            switch (val.type) {
              case "text":
                if (new RegExp(convertKey).test(val.text)) {
                  val.text = key;
                }
                break;
            }
            tempMessage.push(val);
          }
        }

        convertKey = key;

        const tempe = {
          ...this.e,
          raw_message: key,
          msg: undefined,
          message: tempMessage,
        };

        await PluginsLoader.deal(tempe);
        await common.sleep(config.rulesDelay);
      }

      return "return";
    }
    return false;
  }
}
